package replica;

import org.omg.CORBA.BooleanHolder;
import org.omg.CORBA.StringHolder;

import common.CommonTools;
import common.Player;
import player.playerService;

public class PlayerClient {

	private playerService playerService;
	
	private String role;

	public PlayerClient(playerService playerService, String role) {
		this.playerService = playerService;
		this.role = role;
	}

	public String createPlayer(String firstName, String lastName, int age,
			String userName, String password, String clientIpAddress) {
		Player player = new Player(firstName, lastName, age, userName,
				password, clientIpAddress);

		StringHolder status = new StringHolder();
		BooleanHolder result = new BooleanHolder();
		playerService.createPlayerAccount(firstName, lastName, (short) age,
				userName, password, clientIpAddress, result, status);
		if (result.value) {
			return "true";
		} else {
			return "false";
		}
	}

	public String signIn(String userName, String password,
			String clientIpAddress) {
		String content = "player signIn started:" + "userName:" + userName
				+ ";ipAddress:" + clientIpAddress;
		CommonTools.writeLog(role, role, content, "replica");
		StringHolder status = new StringHolder();
		BooleanHolder result = new BooleanHolder();
		playerService.playerSignIn(userName, password, clientIpAddress, result,
				status);
		content = "player signIn succeeded:" + "userName:" + userName
				+ ";ipAddress:" + clientIpAddress + " Status:" + status.value;
		CommonTools.writeLog("replica", "replica", content, "replica");
		if (result.value) {
			return "true";
		} else {
			return "false";
		}

	}

	public String signOut(String userName, String clientIpAddress) {
		String content = "player signOut started:" + "userName:" + userName
				+ ";ipAddress:" + clientIpAddress;
		System.out.println(content);
		CommonTools.writeLog(role, role, content, "replica");
		StringHolder status = new StringHolder();
		BooleanHolder result = new BooleanHolder();
		playerService.playerSignOut(userName, clientIpAddress, result, status);
		content = "player signOut succees status:" + status.value + "userName:"
				+ userName + ";ipAddress:" + clientIpAddress;
		CommonTools.writeLog(role, role, content, "replica");
		if (result.value) {
			return "true";
		} else {
			return "false";
		}
	}

	public String transferAccount(String userName, String password,
			String oldIpAddress, String newIpAddress) {
		String content = "player account transfer started:" + "userName:"
				+ userName + ";oldIpAddress:" + oldIpAddress + ";newIpAddress:"
				+ newIpAddress;
		CommonTools.writeLog(role, role, content, "replica");
		StringHolder status = new StringHolder();
		BooleanHolder result = new BooleanHolder();
		playerService.transferAccount(userName, password, oldIpAddress,
				newIpAddress, result, status);

		content = "player account transfer succees status:" + status.value
				+ " userName:" + userName + ";oldIpAddress:" + oldIpAddress
				+ ";newIpAddress:" + newIpAddress;
		CommonTools.writeLog(role, role, content, "replica");
		if (result.value) {
			return "true";
		} else {
			return "false";
		}
	}
}
